package com.qk.common.util.jwt;

import com.qk.entity.User;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 上官徵羽
 * @version 1.0
 * @since 2025-10-10 16:51
 */
public class JwtUtils {
    private static final String SECRET_KEY = "cWluZ2tl";
    private static final long EXPIRATION_TIME = 12 * 60 * 60 * 10000;
    public static final String CLAIMS_KEY_ONE = "username";
    public static final String CLAIMS_KEY_TWO = "id";

    public static String builderJwts(User user) {
        Map<String, Object> claims = new HashMap<>();
        claims.put(CLAIMS_KEY_ONE, user.getUsername());
        claims.put(CLAIMS_KEY_TWO, user.getId());
        return Jwts.builder()
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME))
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)
                .compact();
    }


    public static Claims parseJwts(String token) {
        return Jwts.parser()
                .setSigningKey(SECRET_KEY)
                .parseClaimsJws(token)
                .getBody();
    }
}
